package com.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Login;

public class LoginInterceptor implements HandlerInterceptor{
	/**
	 * 执行Handler完成之后执行。
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 进入Handler方法之后，返回ModelAndView之前执行
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	/***
	 * 进入Handler方法之前执行。可以用于身份认证、身份授权
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		HttpSession session= request.getSession();
		Login login= (Login)session.getAttribute("login");
		System.out.println("拦截器 loginSession:："+login);
		String url= request.getContextPath();
		if(login==null){
			String path=request.getServletPath();
			String morePath=request.getServletPath();
			if(path.contains("login")){
				return true;
			}
			if(morePath.indexOf("/qd")==-1){
				response.sendRedirect(url+"/loginContent");
			}else{
				response.sendRedirect(url+"/qd/login?xuanxiang=0");
			}
			return false;
		}else{
			return true;
		}
		
	}

}
